package servlets;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import controle.TimeChart;
import controleBD.JoinQueryDAO;

@WebServlet("/GraficoDetalhadoTemp")
public class GraficoDetalhadoTemp extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setContentType("image/png");
		
		OutputStream out = response.getOutputStream();
		
		
		TimeChart timeChart = new TimeChart();
		
		if ((request.getSession().getAttribute("dia") == null) || (request.getSession().getAttribute("dia").equals("Todos"))) {
			timeChart.desenhaGraficoDetalhado(out,"Temperatura", request.getSession().getAttribute("username").toString(),null);
		}else{
			timeChart.desenhaGraficoDetalhado(out,"Temperatura", request.getSession().getAttribute("username").toString(), request.getSession().getAttribute("dia").toString());
		}

	}
	public void imprimir(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		doPost(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter print = response.getWriter();

		print.println("<html>");	
		print.println("<head>");
		print.println("<title>Gr&aacute;fico Temperatura Detalhado</title>");
		print.println("<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\" />");
		print.println("<script type=\"text/javascript\"></script>");
		print.println("<link rel=\"stylesheet\" href=\"styles.css\">");
		print.println("</head>");
		
		print.println("<body>");

		print.println("<p>Gr&aacute;fico representando todos os valores de temperatura.</p>");
		
		print.println("<div id=\"container1\">");
		print.println("<form action=\"GraficoDetalhadoTemp\"method=\"post\">");
		print.println("<img src=\"GraficoDetalhadoTemp\"/><br/>");
		print.println("<table>");
		print.println("<tr>");
		print.println("<td>");
		print.println("<label for=\"name\">Selecionar dia:</label>");
		print.println("</td>");
		print.println("<td>");
		print.println("<select name=\"dia\">");
		
		
		
		JoinQueryDAO jqDao = new JoinQueryDAO();
		Set<String> set = jqDao.retornaDatas("Temperatura");
		
		Iterator<String> iterator = set.iterator();
		print.println("<option value='Todos'>Todos</option>");
		while(iterator.hasNext()) {
	        String setElement = iterator.next();
	        print.println("<option value='"+setElement+"'>"+setElement+"</option>");
	        iterator.remove();
	    }
		
		print.println("</select>");
		print.println("</td>");
		print.println("</tr>");
		print.println("<tr>");
		print.println("<td>");
		print.println("<input type=\"submit\" value=\"OK\" name=\"button\"/>");
		print.println("</td>");
		print.println("</tr>");
		print.println("</table>");
		print.println("</form>");
		print.println("</div>");
		print.println("</body>");
		print.println("</html>");
		
		
		
		request.getSession().setAttribute("dia", request.getParameter("dia"));
		
		
	}
	
}
